package com.typedef.basichumingbirdcontroller;

import com.typedef.helper.HelperContainer;
import com.typedef.helper.UsbConectionHelper;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class ManualCommandsFragment extends Fragment implements OnClickListener {

	private final String TAG = ManualCommandsFragment.class.getSimpleName();
	public static final String ARG_SECTION_NUMBER = "section_number";
	private Button buttonTurnOn = null;
	private Button buttonTurnOff = null;
	private TextView message = null;
	private UsbConectionHelper usbConnHelper = null;

	public ManualCommandsFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_main_manual_commands, container, false);
		message = (TextView) rootView.findViewById(R.id.textView1);
        buttonTurnOn = (Button) rootView.findViewById(R.id.button1);
        buttonTurnOn.setOnClickListener(this);
        buttonTurnOff = (Button) rootView.findViewById(R.id.button2);
        buttonTurnOff.setOnClickListener(this);
        if(HelperContainer.getObject(HelperContainer.USB_CONN_HELPER) != null){
        	this.usbConnHelper = (UsbConectionHelper) HelperContainer.getObject(HelperContainer.USB_CONN_HELPER);
        	message.setText("usbconnhelper no es nulo");
		}
        else{
        	message.setText("esta vacio el contenedor de objetos.");
        }
		return rootView;
	}
	
	@Override
	public void onResume() {
        super.onResume();
	}
	
	@Override
	public void onClick(View arg0) {
		if (arg0.getId() == R.id.button1){
			if(this.usbConnHelper != null){
				this.usbConnHelper.send(new byte[]{0x3e,0x2a,0x3e,0x6d,0x01});
			}
			else{
				if(HelperContainer.getObject(HelperContainer.USB_CONN_HELPER) != null){
		        	this.usbConnHelper = (UsbConectionHelper) HelperContainer.getObject(HelperContainer.USB_CONN_HELPER);
		        	message.setText("usbconnhelper no es nulo");
		        	this.usbConnHelper.send(new byte[]{0x3e,0x2a,0x3e,0x6d,0x01});
				}
			}
		}
		else if(arg0.getId() == R.id.button2){
			if(this.usbConnHelper != null){
				this.usbConnHelper.send(new byte[]{0x3e,0x2a,0x3e,0x6d,0x00});
			}
		}
	}
	
}